<?php

/**
 * Created by PhpStorm.
 * User: kc
 * Date: 2017/3/7
 * Time: 9:59
 */
class AhpBasePageController extends BaseController{
    /**
     * @Author:      kc
     * @DateTime:   2017-03-07 10:00:30
     * @Description: 页面架构列表
     */
    public function pageList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $page = Input::get('page',1);
            $page_size = Input::get('page_size',10);
            $ahp_base_page = AhpBasePage::getBasePage(Input::get('pg_number'),Input::get('pg_name'),Input::get('start_time'),Input::get('end_time'),Input::get('status'),$page,$page_size);
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $ahp_base_page,
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "pageList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-07 10:43:45
     * @Description: 修改页面架构状态
     */
    public function  updatePageStatus(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'page_id' => 'required',
                'status' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $id = explode(',',Input::get('page_id'));
            try{
                AhpBasePage::whereIn('id',$id)->update(['status' => Input::get('status'),'updated_at' => Common::currentTimeString()]);
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "updatePageStatus", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.ahp_update_status_error_1005");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => "ok",
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "updatePageStatus", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-07 13:39:32
     * @Description: 页面内容管理
     */
    public function editPageModEle(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'page_id' => 'required',
                'pg_number' =>'unique:ahp_base_page,pg_number,'.Input::get('page_id'),
                'pg_name' => 'unique:ahp_base_page,pg_name,'.Input::get('page_id'),
                'mod_ele' => 'required',
            )
        );
        do{
            $messages = $validator->messages();
            //验证页面编号唯一
            if($messages->has('pg_number')) {
                $response = Lang::get('ahp_error.pg_number_exit_error_6100');
                break;
            }
            //验证页面名称唯一
            if($messages->has('pg_name')){
                $response = Lang::get('ahp_error.pg_name_exit_error_6101');
                break;
            }
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $mod_ele = json_decode(Input::get('mod_ele'),true);
            $data_one = array();
            $data_two = array();
            /*foreach($mod_ele as $v){
                if(isset($v['ele_id']) && !empty($v['ele_id'])){
                    foreach($v['ele_id'] as $item){
                        $data_one[] = array(
                            'pg_id' => Input::get('page_id'),
                            'mod_id' => $v['mod_id'],
                            'ele_id' => $item,
                            'created_at' => Common::currentTimeString(),
                            'updated_at' => Common::currentTimeString(),
                            'created_by' => $account_user->id,
                        );
                    }
                }else{
                    $data_two[] = array(
                        'pg_id' => Input::get('page_id'),
                        'mod_id' => $v['mod_id'],
                        'ele_id' => 0,
                        'created_at' => Common::currentTimeString(),
                        'updated_at' => Common::currentTimeString(),
                        'created_by' => $account_user->id,
                    );
                }
            }
            $data = array_merge_recursive($data_one,$data_two);
            */
           try{
               $home_page = Input::get('home_page')?Input::get('home_page'):0;
               if($home_page == 1){
                   AhpBasePage::where('home_page',1)->update(['home_page' => 0]);
               }
               $base_page = AhpBasePage::find(Input::get('page_id'));
               $base_page->pg_number = Input::get('pg_number')?Input::get('pg_number'):$base_page->pg_number;
               $base_page->pg_name = Input::get('pg_name')?Input::get('pg_name'):$base_page->pg_name;
               $base_page->home_page = $home_page;
               $base_page->version = Input::get('version')?Input::get('version'):$base_page->version;
               $base_page->updated_at = Common::currentTimeString();
               $base_page->save();
               $r_id = AhpPgModRelation::where('pg_id',Input::get('page_id'))->select('id')->get()->toArray();
               AhpPgModRelation::whereIn('id',$r_id)->delete();
               AhpRModEleRelation::whereIn('r_id',$r_id)->delete();
               foreach($mod_ele as $v){
                   $ahp_pg_mod = AhpPgModRelation::insertGetId([
                           'pg_id' => Input::get('page_id'),
                           'mod_id' => $v['mod_id'],
                           'created_at' => Common::currentTimeString(),
                            'updated_at' => Common::currentTimeString(),
                            'created_by' => $account_user->id,
                       ]);
                   if(isset($v['ele_id']) && !empty($v['ele_id'])){
                       foreach($v['ele_id'] as $item){
                           AhpRModEleRelation::insert([
                               'r_id' => $ahp_pg_mod,
                               'ele_id' => $item,
                               'created_at' => Common::currentTimeString(),
                               'updated_at' => Common::currentTimeString(),
                               'created_by' => $account_user->id,
                           ]);
                       }
                   }
               }
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "/editPageContent", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.ahp_create_error_1003");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "/editPageContent", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-07 17:27:20
     * @Description: 页面内容详情
     */
    public function pageDetails(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'page_id' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $ahp_pg_mod_ele = AhpPgModRelation::getPgModEleDetails(Input::get('page_id'));
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $ahp_pg_mod_ele
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "pageDetails", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}